home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / Multi-Texture CVA / multitex_cva.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  7.8 KB  |  346 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "agl.h"
  6.  
  7. #include <Fonts.h>
  8. #include <Events.h>
  9. #include <Windows.h>
  10. #include <Dialogs.h>
  11. #include <timer.h>
  12.  
  13. #define TRI_STRIP_COUNT     20
  14. #define TRI_STRIP_SIZE      512
  15. #define TRI_STRIP_REPEAT    2
  16. #define WINDOW_WIDTH        300
  17. #define WINDOW_HEIGHT       300
  18.  
  19. #define SWAP_COUNT          10
  20.  
  21. static GLubyte tmap[64] = {
  22.     0, 255, 0, 255, 0, 255, 0, 255,
  23.     255, 0, 255, 0, 255, 0, 255, 0,
  24.     0, 255, 0, 255, 0, 255, 0, 255,
  25.     255, 0, 255, 0, 255, 0, 255, 0,
  26.     0, 255, 0, 255, 0, 255, 0, 255,
  27.     255, 0, 255, 0, 255, 0, 255, 0,
  28.     0, 255, 0, 255, 0, 255, 0, 255,
  29.     255, 0, 255, 0, 255, 0, 255, 0};
  30.  
  31. static GLubyte t2map[48] = {
  32.     255, 0, 0,  0, 255, 0,  0, 0, 255,  0, 0,   0,
  33.       0, 0, 0,  255, 0, 0,  0, 255, 0,  0, 0, 255,
  34.     0, 0, 255,    0, 0, 0,  255, 0, 0,  0, 255, 0,
  35.     0, 255, 0,  0, 0, 255,    0, 0, 0,  255, 0, 0};
  36.  
  37. static GLfloat  varray[TRI_STRIP_COUNT][TRI_STRIP_SIZE * 2];
  38. static GLubyte  carray[TRI_STRIP_SIZE * 4];
  39. static GLfloat  tarray[TRI_STRIP_SIZE * 2];
  40. static GLushort indices[TRI_STRIP_SIZE];
  41.  
  42. static GLuint   str_base;
  43.  
  44. #define AGL_FONT_START  31
  45. #define AGL_FONT_END    128
  46.  
  47. #define AGL_FONT_TYPE   kFontIDNewYork
  48. #define AGL_FONT_SIZE   10
  49. #define AGL_FONT_STYLE  normal
  50.  
  51. static double seconds(void)
  52. {
  53.    UnsignedWide _time;
  54.    
  55.    Microseconds(&_time);
  56.    
  57.    return 4294.967296 * _time.hi + 0.000001 * _time.lo;
  58. }
  59.  
  60. static void init_str(void)
  61. {
  62.     str_base = glGenLists(AGL_FONT_END - AGL_FONT_START);
  63.     
  64.     aglUseFont(aglGetCurrentContext(), AGL_FONT_TYPE, AGL_FONT_STYLE, AGL_FONT_SIZE, AGL_FONT_START, AGL_FONT_END, str_base);
  65.     
  66.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  67.     glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  68. }
  69.  
  70. static void draw_str(char *str)
  71. {
  72.     int len, i;
  73.     GLubyte list_str[128];
  74.     
  75.     len = strlen(str);
  76.     for(i = 0; i < len; i++) list_str[i] = str[i] - AGL_FONT_START;
  77.     
  78.     glListBase(str_base);
  79.     glCallLists(len, GL_UNSIGNED_BYTE, list_str);
  80. }
  81.  
  82. static void setup(void)
  83. {
  84.     GLint i, j;
  85.     
  86.     init_str();
  87.     
  88.     /* Build tri-strips */
  89.     for(i = 0; i < TRI_STRIP_SIZE; i++) indices[i] = i;
  90.     
  91.     for(i = 0; i < TRI_STRIP_COUNT; i++)
  92.     {
  93.         for(j = 0; j < (2 * TRI_STRIP_SIZE - 3); j += 4)
  94.         {
  95.             GLfloat x, y1, y2;
  96.             
  97.             x  = -0.9f + 1.8f * ((GLfloat) j / (GLfloat) (2 * TRI_STRIP_SIZE - 4));
  98.             y1 = -0.9f + 1.8f * ((GLfloat) i / (GLfloat) TRI_STRIP_COUNT);
  99.             y2 = -0.9f + 1.8f * ((GLfloat) (i + 1) / (GLfloat) TRI_STRIP_COUNT);
  100.  
  101.             varray[i][j    ] = x;
  102.             varray[i][j + 1] = y1; 
  103.  
  104.             varray[i][j + 2] = x;
  105.             varray[i][j + 3] = y2;
  106.         }
  107.     }
  108.     
  109.     for(i = 0; i < (4 * TRI_STRIP_SIZE - 7); i += 8)
  110.     {
  111.         carray[i    ] = 1.0;
  112.         carray[i + 1] = 0.0;
  113.         carray[i + 2] = 0.0;
  114.         carray[i + 3] = 1.0;
  115.         
  116.         carray[i + 4] = 0.0;
  117.         carray[i + 5] = 1.0;
  118.         carray[i + 6] = 0.0;
  119.         carray[i + 7] = 1.0;
  120.     }
  121.     
  122.     for(i = 0; i < (2 * TRI_STRIP_SIZE - 3); i += 4)
  123.     {
  124.         GLfloat s1, s2, t1, t2;
  125.         
  126.         s1  = 0.0f + 2.0f * ((GLfloat) i / (GLfloat) (2 * TRI_STRIP_SIZE - 4));
  127.         s2  = 0.0f + 2.0f * ((GLfloat) (i + 1) / (GLfloat) (2 * TRI_STRIP_SIZE - 4));
  128.         
  129.         t1  = 0.0;
  130.         t2  = 1.0;
  131.         
  132.         tarray[i    ] = s1;
  133.         tarray[i + 1] = t1;
  134.         
  135.         tarray[i + 2] = s2;
  136.         tarray[i + 3] = t2;
  137.     }
  138.     
  139.     /* Coords and colors */
  140.     glVertexPointer(2, GL_FLOAT, 8, varray[0]);
  141.     glColorPointer(4, GL_UNSIGNED_BYTE, 0, carray);
  142.     
  143.     glEnableClientState(GL_VERTEX_ARRAY);
  144.     glEnableClientState(GL_COLOR_ARRAY);
  145.     
  146.     /* Texture unit 0 */
  147.     glActiveTextureARB(GL_TEXTURE0_ARB);
  148.     glClientActiveTextureARB(GL_TEXTURE0_ARB);
  149.     
  150.     glTexCoordPointer(2, GL_FLOAT, 8, tarray);
  151.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  152.         
  153.     glBindTexture(GL_TEXTURE_2D, 0);
  154.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  155.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  156.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  157.     glTexImage2D(GL_TEXTURE_2D, 0, 1, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, tmap);
  158.     
  159.     glEnable(GL_TEXTURE_2D);
  160.  
  161.     /* Texture unit 1 */
  162.     glActiveTextureARB(GL_TEXTURE1_ARB);
  163.     glClientActiveTextureARB(GL_TEXTURE1_ARB);
  164.     
  165.     glTexCoordPointer(2, GL_FLOAT, 8, tarray);
  166.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  167.  
  168.     glBindTexture(GL_TEXTURE_2D, 1);
  169.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  170.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  171.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  172.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, t2map);
  173.     
  174.     glEnable(GL_TEXTURE_2D);
  175.     
  176.     /* Set projection matrix */
  177.     glMatrixMode(GL_PROJECTION);
  178. }
  179.  
  180. static void draw(void)
  181. {
  182.     GLint i, j;
  183.     GLint swap = 1;
  184.     char num_str[200];
  185.     GLdouble t1 = 0.0, t2 = 0.0, t, fps = 1.0;
  186.     
  187.     glLockArraysEXT(0, TRI_STRIP_SIZE);
  188.     
  189.     t1 = seconds();
  190.     
  191.     if(TRI_STRIP_COUNT == 1)
  192.     {
  193.         while(!Button())
  194.         {
  195.             if(swap == SWAP_COUNT) glClear(GL_COLOR_BUFFER_BIT);
  196.             
  197.             for(j = 0; j < TRI_STRIP_REPEAT; j++)
  198.             {
  199.                 glDrawElements(GL_TRIANGLE_STRIP, TRI_STRIP_SIZE, GL_UNSIGNED_SHORT, indices);
  200.             }
  201.             
  202.             if(swap == SWAP_COUNT)
  203.             {
  204.                 t2 = seconds();
  205.                 t = t2 - t1;
  206.                 t1 = t2;
  207.                 
  208.                 fps = (t > 0.00001 ? (GLdouble) SWAP_COUNT / t : 1.0);
  209.                 
  210.                 glOrtho(0.0, WINDOW_WIDTH - 15, 0, WINDOW_HEIGHT - 15, -100.0, 100.0);
  211.                 
  212.                 glColor3f(1.0, 0.0, 0.0);
  213.                 glRasterPos3f(5, WINDOW_HEIGHT - 25, 0.0);
  214.                 
  215.                 sprintf(num_str, "%dx%d, Tri/Sec: %u, FPS: %f",
  216.                     WINDOW_WIDTH, WINDOW_HEIGHT,
  217.                     (GLuint) ((GLdouble) ((TRI_STRIP_SIZE - 2) * TRI_STRIP_COUNT * TRI_STRIP_REPEAT) * fps), fps);
  218.                     
  219.                 draw_str(num_str);
  220.                 
  221.                 glLoadIdentity();
  222.                 
  223.                 aglSwapBuffers(aglGetCurrentContext());
  224.                 swap = 0;
  225.             }
  226.             
  227.             swap++;
  228.         }
  229.     }
  230.     else
  231.     {
  232.         while(!Button())
  233.         {
  234.             if(swap == SWAP_COUNT) glClear(GL_COLOR_BUFFER_BIT);
  235.             
  236.             for(i = 0; i < TRI_STRIP_COUNT; i++)
  237.             {
  238.                 glVertexPointer(2, GL_FLOAT, 8, varray[i]);
  239.                 
  240.                 for(j = 0; j < TRI_STRIP_REPEAT; j++)
  241.                 {
  242.                     glDrawElements(GL_TRIANGLE_STRIP, TRI_STRIP_SIZE, GL_UNSIGNED_SHORT, indices);
  243.                 }
  244.             }
  245.             
  246.             if(swap == SWAP_COUNT)
  247.             {
  248.                 t2 = seconds();
  249.                 t = t2 - t1;
  250.                 t1 = t2;
  251.                 
  252.                 fps = (t > 0.00001 ? (GLdouble) SWAP_COUNT / t : 1.0);
  253.  
  254.                 glOrtho(0.0, WINDOW_WIDTH - 15, 0, WINDOW_HEIGHT - 15, -100.0, 100.0);
  255.                 
  256.                 glColor3f(1.0, 0.0, 0.0);
  257.                 glRasterPos3f(5, WINDOW_HEIGHT - 25, 0.0);
  258.                 
  259.                 sprintf(num_str, "%dx%d, Tri/Sec: %u, FPS: %f",
  260.                     WINDOW_WIDTH, WINDOW_HEIGHT,
  261.                     (GLuint) ((GLdouble) ((TRI_STRIP_SIZE - 2) * TRI_STRIP_COUNT * TRI_STRIP_REPEAT) * fps), fps);
  262.                     
  263.                 draw_str(num_str);
  264.                 
  265.                 glLoadIdentity();
  266.  
  267.                 aglSwapBuffers(aglGetCurrentContext());
  268.                 swap = 0;
  269.             }
  270.             
  271.             swap++;
  272.         }
  273.     }
  274.     
  275.     /* One more swap for good luck */
  276.     aglSwapBuffers(aglGetCurrentContext());
  277. }
  278.  
  279. int main(void)
  280. {
  281.     Rect           rect;
  282.     CGrafPtr       win;
  283.     GLint          attrib[] = { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_NONE };
  284.     AGLPixelFormat fmt;
  285.     AGLContext     ctx;
  286.  
  287.     /* Initialize Macintosh system */
  288.     InitGraf(&qd.thePort);
  289.     InitFonts();
  290.     FlushEvents(everyEvent, 0);
  291.     InitWindows();
  292.     InitMenus();
  293.     TEInit();
  294.     InitDialogs(0L);
  295.     InitCursor();
  296.  
  297.     /* Create a window */
  298.     {
  299.         int offset = 50;
  300.         SetRect(&rect, offset, 50, offset + WINDOW_WIDTH, 50 + WINDOW_HEIGHT);
  301.     }
  302.  
  303.     win = (CGrafPtr) NewCWindow(NULL, &rect, "\pComp Array Test", false,
  304.                                 documentProc, (WindowPtr) -1L, true, 0L);
  305.     if(win == NULL) return 1;
  306.  
  307.     ShowWindow((GrafPort *) win);
  308.  
  309.     /* Never unload a code module */
  310.     aglConfigure(AGL_RETAIN_RENDERERS, GL_TRUE);
  311.  
  312.     /* Choose pixel format */
  313.     fmt = aglChoosePixelFormat(NULL, 0, attrib);
  314.     if(fmt == NULL) return 1;
  315.  
  316.     /* Create an AGL context */
  317.     ctx = aglCreateContext(fmt, NULL);
  318.     if(ctx == NULL) return 1;
  319.  
  320.     /* Attach the context to the window */
  321.     if(!aglSetDrawable(ctx, win)) return 1;
  322.  
  323.     /* Make this context current */
  324.     aglSetCurrentContext(ctx);
  325.  
  326.     /**/
  327.     setup();
  328.     
  329.     /* Draw some stuff */
  330.     draw();
  331.     
  332.     /* Dispose of the pixel format */
  333.     aglDestroyPixelFormat(fmt);
  334.  
  335.     /* Make the context not current, set it's drawable to NULL, and destroy it */
  336.     aglSetCurrentContext(NULL);
  337.     aglSetDrawable(ctx, NULL);
  338.     aglDestroyContext(ctx);
  339.  
  340.     /* Destroy the window */
  341.     DisposeWindow((WindowPtr) win);
  342.  
  343.     return 0;
  344. }
  345.  
  346.